package com.uyayo.oms.system.controller;


import com.uyayo.framework.common.annotation.MethodName;
import com.uyayo.framework.message.model.Result;
import com.uyayo.oms.system.biz.ParamBiz;
import com.uyayo.oms.system.vo.SysIconVO;
import com.uyayo.framework.pagehelper.pojo.PageBean;
import com.uyayo.framework.pagehelper.pojo.PageResult;
import com.uyayo.system.authority.entity.SysUser;
import com.uyayo.system.config.entity.SysIcon;
import com.uyayo.system.config.model.SysIconCondition;
import com.uyayo.system.config.service.SysIconService;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.apache.shiro.subject.Subject;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.MessageSource;
import org.springframework.stereotype.Controller;
import org.springframework.validation.Errors;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import java.util.Date;


/**
 * 
 * CSS图标
 * @author 乐乐科技.胡逢君
 * @date 2015-4-13下午4:03:35
 * @version 1.0
 */
@Controller
@RequestMapping("/system/config/icon")
public class SysIconController {

	private final SysIconService iconService;

	private final ParamBiz paramBiz;

	private  final MessageSource messageSource;

	private final Logger logger = LoggerFactory.getLogger(this.getClass());

	@Autowired
	public SysIconController(SysIconService iconService, ParamBiz paramBiz, MessageSource messageSource) {
		this.iconService = iconService;
		this.paramBiz = paramBiz;
		this.messageSource = messageSource;
	}

	@RequestMapping(value="index",method= RequestMethod.GET)
	@MethodName(value="系统管理->系统配置->预置图标管理")
	@RequiresPermissions("system:config:icon:index")
	public String index() throws Exception {
		return paramBiz.getManagerTemplatesPrefix()+"system/config/icon/index";
	}

	@RequestMapping(value="select",method= RequestMethod.GET)
	@MethodName(value="系统管理->系统配置->预置图标管理->选择图标样式")
	public String select() throws Exception {
		return paramBiz.getManagerTemplatesPrefix()+"system/config/icon/select";
	}

	@ResponseBody
	@RequestMapping(value="grid",method=RequestMethod.GET)
	@MethodName(value="系统管理->系统配置->预置图标管理->查询列表",type=2)
	@RequiresPermissions("system:config:icon:grid")
	public PageResult<SysIcon> selectIcon(@RequestParam(value = "offset",required = false, defaultValue = "1") Integer start, //第多少行之后开始
										  @RequestParam(value = "limit", required = false, defaultValue = "10") Integer pageSize, String sort, String order, SysIconCondition sysIconCondition) throws Exception {
		PageBean<SysIcon> pageBean=new PageBean<>();
		try {
			pageBean = iconService.selectIconByPager((start / pageSize) + 1, pageSize,sort,order,sysIconCondition);
		} catch (Exception e) {
			e.printStackTrace();		
		}
		PageResult<SysIcon> pageResult = new PageResult<>();
		pageResult.setRows(pageBean.getList());
		pageResult.setTotal(pageBean.getTotalCount());
		return pageResult;
	}

	/**
	 * 进入CSS图标表单
	 */
	@RequestMapping(value = "add", method = RequestMethod.GET)
	public String add() {
		return paramBiz.getManagerTemplatesPrefix()+"system/config/icon/add";
	}
	
	/**
	 * 新建一个CSS图标
	 */
	@ResponseBody
	@RequestMapping(value = "add", method = RequestMethod.POST)
	@MethodName(value="系统管理->系统配置->预置图标管理->新增图标",type=2)
	synchronized public Result add(HttpServletRequest request, SysIconVO sysIconVO, Errors errors) {
		try {
			if (errors.hasErrors()) {
				return Result.errorAndMsg(errors.getFieldError().getDefaultMessage());
			}
			Subject subject = SecurityUtils.getSubject();
			SysUser sysUser = (SysUser) subject.getPrincipal();

			SysIcon sysIcon = new SysIcon();
			sysIcon.setClazz(sysIconVO.getClazz());
			sysIcon.setWidth(sysIconVO.getWidth());
			sysIcon.setHeight(sysIconVO.getHeight());
			sysIcon.setCreatedTime(new Date());
			sysIcon.setCreatedBy(sysUser.getLoginname());
			sysIcon.setType(sysIconVO.getType());
			sysIcon.setSeq(sysIconVO.getSeq());
			sysIcon.setRemark(sysIconVO.getRemark());
			sysIcon.setDescription(sysIconVO.getDescription());
			int countSave = iconService.saveIcon(sysIcon);
			if(countSave<=0){
				String message = messageSource.getMessage("save.fail",new Object[]{}, request.getLocale());
				logger.info(message);
				return Result.build(400,message);			}
		} catch (Exception e) {
			String message = messageSource.getMessage("server.error.500",new Object[]{}, request.getLocale());
			logger.error(message,e);
			return Result.build(500, message);
		}
		String message = messageSource.getMessage("save.success",new Object[]{}, request.getLocale());

		return Result.successAndMsg(message);
	}


	@ResponseBody
	@RequestMapping(value = "update", method = RequestMethod.GET)
	@RequiresPermissions("system:config:icon:update")
	public ModelAndView update(ModelAndView modelAndView, HttpServletRequest request, Integer id) {
		SysIcon sysIcon = null;
		try {
			sysIcon = iconService.getIconById(id);
		} catch (Exception e) {
			String message = messageSource.getMessage("server.error.500",new Object[]{}, request.getLocale());
			logger.error(message,e);
		}
		modelAndView.addObject("sysIcon",sysIcon);
		modelAndView.setViewName(paramBiz.getManagerTemplatesPrefix()+"system/config/icon/edit");
		return modelAndView;
	}

	/**
	 * 新建一个CSS图标
	 */
	@ResponseBody
	@RequestMapping("update")
	@RequiresPermissions("system:config:icon:update")
	@MethodName(value="系统管理->系统配置->预置图标管理->修改图标",type=2)
	synchronized public Result updateIcon(HttpServletRequest request, SysIconVO sysIconVO, Errors errors) {
		try {
			if (errors.hasErrors()) {
				return Result.errorAndMsg(errors.getFieldError().getDefaultMessage());
			}
			Subject subject = SecurityUtils.getSubject();
			SysUser sysUser = (SysUser) subject.getPrincipal();

			SysIcon sysIcon = new SysIcon();
			sysIcon.setId(sysIconVO.getId());
			sysIcon.setClazz(sysIconVO.getClazz());
			sysIcon.setWidth(sysIconVO.getWidth());
			sysIcon.setHeight(sysIconVO.getHeight());
			sysIcon.setModifiedTime(new Date());
			sysIcon.setModifiedBy(sysUser.getLoginname());
			sysIcon.setType(sysIconVO.getType());
			sysIcon.setSeq(sysIconVO.getSeq());
			sysIcon.setRemark(sysIconVO.getRemark());
			sysIcon.setDescription(sysIconVO.getDescription());
			int count = iconService.updateIcon(sysIcon);
			if(count<=0){
				String message = messageSource.getMessage("save.fail",new Object[]{}, request.getLocale());
				logger.info(message);
				return Result.build(400,message);			}
		} catch (Exception e) {
			String message = messageSource.getMessage("server.error.500",new Object[]{}, request.getLocale());
			logger.error(message,e);
			return Result.build(500, message);
		}
		String message = messageSource.getMessage("save.success",new Object[]{}, request.getLocale());

		return Result.successAndMsg(message);
	}
	
	/**
	 * 删除图标
	 */
	@ResponseBody
	@RequestMapping("delete")
	@RequiresPermissions("system:config:icon:update")
	@MethodName(value="系统管理->系统配置->预置图标管理->删除图标",type=2)
	public Result deleteIcon(HttpServletRequest request, Integer id) {
		try {
			if (id==null) {
				String message = messageSource.getMessage("parameter.error",new Object[]{}, request.getLocale());
				logger.error(message);
				return Result.build(400,message);
			}
			int deleteCount = iconService.deleteIcon(id);
			if(deleteCount<=0){
				String message = messageSource.getMessage("delete.fail",new Object[]{}, request.getLocale());
				logger.error(message);
				return Result.build(400,message);
			}
		} catch(Exception e) {
			String message = messageSource.getMessage("server.error.500",new Object[]{}, request.getLocale());
			logger.error(message,e);
			return Result.build(500,message);
		}
		String message = messageSource.getMessage("delete.success",new Object[]{}, request.getLocale());
		logger.info(message);
		return Result.success(200,message);
		
	}
	
}
